package com.example.springbook.dao;

import com.example.springbook.bean.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookDao {
    @Insert("insert into book_detail(bookid,bookname,author,lendnum,surplus) values (#{bookid},#{bookname},#{author},#{lendnum},#{surplus})")
    void addBook(Book book);

    @Update("update book_detail set bookname=#{bookname},author=#{author},lendnum=#{lendnum},surplus=#{surplus} where bookid=#{bookid}")
    void updateBook(Book book);

    @Update("update book_detail set lendnum=lendnum+1,surplus=surplus-1 where (select surplus where bookid=#{bookid})>0")
    void lendBook(int bookid);
    @Update("update book_detail set lendnum=lendnum-1,surplus=surplus+1 where bookid=#{bookid}")
    void returnBook(int bookid);
    @Delete("delete from book_detail where bookid=#{bookid}")
    void deleteBook(int bookid);

    @Select("select surplus from book_detail where bookid=#{bookid}")
    int findSurplus(@Param("bookid") int bookid);

    @Select("select * from book_detail where bookname=#{bookName}")
    List<Book> findByBookName(@Param("bookName") String bookName);

    @Select("select * from book_detail")
    List<Book> findBookAll();

}
